// pages/farmerend/farmerend.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        currentPage: 1,
        orderState: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        // that.orderList(that.data.currentPage, that.data.orderState);
        // app.globalData.id
    },
    onShow:function(){
        var that = this;
        that.setData({
            currentPage: 1,
            groupgood: [],
        })
        that.orderList(that.data.currentPage, that.data.orderState);
    },
    orderList: function(currentPage, orderState) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/Storemanage/my_order", {
            page: currentPage,
            store_id: wx.getStorageSync("userType").storeid,
            order_state: orderState
        }, function(res) {
            utils.hideLoading();
            var allpost = that.data.groupgood;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                allpost.push(res.datas[i])
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                groupgood: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.orderList(page, that.data.orderState);
        }
    },
    //tab切换
    tab: function(e) {
        var that = this;
        var stateId = e.currentTarget.dataset.stateid;
        var dataId = e.currentTarget.id;
        var curHdIndex = dataId;
        that.setData({
            curHdIndex: curHdIndex,
            orderState: stateId,
            currentPage: 1,
            groupgood: [],

        })
        that.orderList(that.data.currentPage, that.data.orderState);
    },
    //发货
    onFarmerdeliveryTap: function(e) {
        var id = e.currentTarget.dataset.id;
        // 代发货   发货详情     //待确认  物流页面
        // wx.navigateTo({
        //     url: '../farmerend/farmerdelivery/farmerdelivery',
        // })
        // 代售后   售后详情
        wx.navigateTo({
            url: '../farmerend/saledetails/saledetails?id=' + id,
        })
    },

    //跳入详情我的果农页面
    onFarmerendpersonalTap: function(e) {

        wx.redirectTo({
            url: '../farmerend/farmerendpersonal/farmerendpersonal',
        })
    },

    //返回用户商城首页
    onIndexTap: function() {
        wx.reLaunch({
            url: '../index/index',
        })
    },

})